package xq.gwt.ui.widgets.property;

import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

import xq.gwt.mvc.controller.BooleanPropertyController;
import xq.gwt.mvc.model.BooleanPropertyModel;
import xq.gwt.mvc.view.BooleanPropertyView;

public class CheckBoxItemPropertyPresenter extends FormItemPropertyPresenter implements BooleanPropertyView{

	protected CheckboxItem checkboxItem;
	protected BooleanPropertyController booleanController;
	
	public CheckBoxItemPropertyPresenter(){
		checkboxItem = new CheckboxItem();		
		formItem = checkboxItem;
		init();
	}
	
	public CheckBoxItemPropertyPresenter(CheckboxItem checkboxItem){
		this.checkboxItem = checkboxItem;
		formItem = checkboxItem;
		init();
	}
	
	protected void init(){		
		checkboxItem.addChangedHandler(new ChangedHandler() {			
			@Override
			public void onChanged(ChangedEvent event) {
				notifyViewChangedListeners();				
			}
		});
		booleanController = new BooleanPropertyController(this);
		super.controller = controller;
	}
	
	public void setModel(BooleanPropertyModel model){
		booleanController.setPropertyModel(model);
	}
	
	@Override
	public void setBoolean(Boolean val) {
		if(val == null){
			this.checkboxItem.setValue(false);
		}
		else{
			this.checkboxItem.setValue(val);
		}
		
		
	}

	@Override
	public Boolean getBoolean() {
		return this.checkboxItem.getValueAsBoolean();		
	}
	
}
